﻿using System;

namespace HW_3.c2
{
    //Tick事件的参数类型
    public class TickEventArgs : EventArgs
    {
        public string time { get; set; }
    }
    //Alarm事件的参数类型
    public class AlarmEventArgs : EventArgs
    {
        public string ringTime { get; set; }
    }
    //Tick委托类型
    public delegate void TickEventHandler(object sender, TickEventArgs e);
    //Alarm委托类型
    public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
    //事件源（闹钟）
    public class Clock
    {
        //定义事件
        public event TickEventHandler Tick;
        public event AlarmEventHandler Alarm;
        //定义Tick事件处理函数
        public void Process(string t)
        {
            TickEventArgs tickArgs = new TickEventArgs();
            while (true)
            {
                
                System.Threading.Thread.Sleep(1000);
                DoRing(t);
                tickArgs.time = DateTime.Now.ToLongTimeString().ToString();
                Tick(this, tickArgs);
            }
        }
        //定义Alarm事件处理函数
        public void DoRing(string t)
        {
            AlarmEventArgs alarmArgs = new AlarmEventArgs();
            alarmArgs.ringTime = t;
            if(DateTime.Now.ToLongTimeString().ToString() == t)
            {
                Alarm(this, alarmArgs);
            }
        }
    }

    class Program
    {
        static void ShowTick(object sender, TickEventArgs e)
        {
            Console.WriteLine("Tick, Tick, it's {0} now...", e.time);
        }
        static void ShowRing(object sender, AlarmEventArgs e)
        {
            Console.WriteLine("Ring! Ring! It's {0} now!!!!!",e.ringTime);
        }
        static void Main(string []args)
        {
            Console.WriteLine("闹钟程序，请输入闹钟时间（示例(时：分：秒，注意中英文标点符号)：16:40:20）");
            string time = Console.ReadLine();
            Clock c = new Clock();
            c.Tick += ShowTick;
            c.Alarm += ShowRing;
            c.Process(time);
        }
    }
}
